from django.shortcuts import render, redirect, reverse, HttpResponse
from chart_web.form import account
from django.http import JsonResponse
from chart_web import models
from uuid import uuid4
from utils import Md5
import datetime


def register_login(request):
    '''注册登录'''
    dic = {
        'status': 1,
        'msg': ''
    }

    if request.method == 'GET':
        return render(request, 'register_login.html')
    form = account.register_login(request, data=request.POST)
    if form.is_valid():
        telephone = form.cleaned_data.get('telephone')
        exist = models.UserInfo.objects.filter(telephone=telephone, is_show=False).exists()
        if not exist:
            # 注册+存session
            username = str(uuid4()).split('-')[-1]
            form.instance.username = username
            instance = form.save()
            request.session['user_id'] = instance.id
            # 创建交易记录
            policy_obj = models.PricePolicy.objects.filter(category=1, title='青春无敌版').first()
            models.Transaction.objects.create(
                status=1,
                order=Md5.uid(str(uuid4()).split('-')[-1]),
                userinfo=instance,
                price_policy=policy_obj,
                count=0,
                price=0,
                start_datetime=datetime.datetime.now()
            )
            return JsonResponse(dic)
        else:
            return JsonResponse(dic)
    dic['msg'] = form.errors
    dic['status'] = 0
    return JsonResponse(dic)


def tencent_sms(request):
    '''短信发送'''
    dic = {
        'status': 1,
        'msg': ''
    }
    # 进行form验证
    # 通过前端传送过来的tpl，去setting里面查询当前模板对应的值，
    if request.method == 'GET':
        return render(request, 'register_login.html')
    form = account.tencent_sms(request, data=request.POST)
    if form.is_valid():
        return JsonResponse(dic)
    dic['msg'] = form.errors
    dic['status'] = 0
    return JsonResponse(dic)


def home(request):
    '''首页'''
    return render(request, 'home.html')


def personal_center(request):
    '''个人中心'''
    return render(request, 'personal.html')


def logout(request):
    request.session.flush()
    return redirect('home')


def modify_telephone(request, user_id):
    '''修改手机号'''
    dic = {
        'status': 1,
        'msg': ''
    }
    if request.method == 'GET':
        return render(request, 'personal.html')
    # 进行表单验证
    form = account.modify_telephone(data=request.POST)
    if form.is_valid():
        # 通过进行修改
        request.chart_web.user.telephone = form.cleaned_data.get('telephone')
        request.chart_web.user.save()
        return JsonResponse(dic)
    # 不通过返回错误信息
    dic['status'] = 0
    dic['msg'] = form.errors
    return JsonResponse(dic)


def modify_password(request, user_id):
    '''修改密码'''
    dic = {
        'status': 1,
        'msg': ''
    }
    if request.method == 'GET':
        return render(request, 'personal.html')
    # 进行表单验证
    form = account.modify_password(request, data=request.POST)
    if form.is_valid():
        # 通过进行修改
        request.chart_web.user.password = form.cleaned_data.get('password')
        request.chart_web.user.save()
        return JsonResponse(dic)
    # 不通过返回错误信息
    dic['status'] = 0
    dic['msg'] = form.errors
    return JsonResponse(dic)


def modify_username(request, user_id):
    '''修改用户名'''
    dic = {
        'status': 1,
        'msg': ''
    }
    if request.method == 'GET':
        return render(request, 'personal.html')
    # 进行表单验证
    form = account.modify_username(request, data=request.POST)
    if form.is_valid():
        # 通过进行修改
        request.chart_web.user.username = form.cleaned_data.get('username')
        request.chart_web.user.save()
        return JsonResponse(dic)
    # 不通过返回错误信息
    dic['status'] = 0
    dic['msg'] = form.errors
    return JsonResponse(dic)


def modify_icon(request, user_id):
    '''
 头像改变
 '''
    dic = {
        'status': 1,
        'msg': ""
    }
    if request.method == 'GET':
        return render(request, 'personal.html')

    icon = request.FILES.get('icon')
    request.chart_web.user.icon = icon
    request.chart_web.user.save()
    return JsonResponse(dic)
